package com.jim.study.delicacy_bjx.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jim.study.delicacy_bjx.dao.DelicacyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class DelicacyService {

    @Autowired
    private DelicacyMapper delicacyMapper;


    /**
     * 条件查询美食
     * @param data
     * @return
     */

    public List<Map<String,String>> selectDelicacy(JSONObject data){
//        List<Map<String, String>> tmp = new ArrayList<>();             //接收前端的数据
//        List<Map<String, String>> target = new ArrayList<>();
        JSONArray arrayArea = data.getJSONArray("city_select");
        JSONArray arrayCuisine = data.getJSONArray("cuisine_select");
        JSONArray arrayKeyword = data.getJSONArray("keyword_select");
        JSONArray arrayPrice = data.getJSONArray("price_area");
        List<String> listPrice = new ArrayList<>();
        List<String> listArea = new ArrayList<>();
        List<String> listCuisine = new ArrayList<>();
        List<String> listKeyword = new ArrayList<>();
        arrayPrice.forEach(p -> {
            String price = p.toString();
            listPrice.add(price);
        });
        //对价格区间进行逻辑上的处理
        String low = listPrice.get(0);
        if(low.isEmpty()){low="0";}
        Integer Low = Integer.parseInt(low);
        String high = listPrice.get(1);
        if(high.isEmpty()){high="0";}
        if(high.equals("0")){high="99999"; }
        Integer High = Integer.parseInt(high);
        //提取前端设置的条件
        arrayArea.forEach(p -> {
            String area = p.toString();
            listArea.add(area);
        });
        arrayCuisine.forEach(p -> {
            String cuisine = p.toString();
            listCuisine.add(cuisine);
        });
        arrayKeyword.forEach(p -> {
            String keyword = p.toString();
            listKeyword.add(keyword);
        });

        return selectDelicacyByCondition(listArea,listCuisine,listKeyword,Low,High);
    }

    /**
     * 智能推荐美食
     * @param
     * @return
     */
    public List<Map<String, String>> selectPreferenceDelicacy(String username){
        List<String> listKeyword = new ArrayList<>();
        List<String> listArea = new ArrayList<>();
        List<String> listCuisine = new ArrayList<>();
        List<Map<String, String>> list = delicacyMapper.getUserPreference(username);
        for (Map item : list){
            if(item.get("TYPE").equals("0")){
                listArea.add(item.get("PREFERENCE_ID").toString());
            }
            if(item.get("TYPE").equals("1")){
                listCuisine.add(item.get("PREFERENCE_ID").toString());
            }
            if(item.get("TYPE").equals("2")){
                listKeyword.add(item.get("PREFERENCE_ID").toString());
            }
        }
        return selectDelicacyByCondition(listArea,listCuisine,listKeyword,0,99999);
    }


    /**
     * 封装条件选择菜品的方法
     * @param listArea
     * @param listCuisine
     * @param listKeyword
     * @param Low
     * @param High
     * @return
     */

    public List<Map<String, String>> selectDelicacyByCondition(List<String> listArea,List<String> listCuisine,List<String> listKeyword,Integer Low,Integer High){
        List<Map<String, String>> tmp = new ArrayList<>();
        List<Map<String, String>> target = new ArrayList<>();
        List<Map<String, String>> resultArea = new ArrayList<>();
        List<Map<String, String>> resultCuisine = new ArrayList<>();
        List<Map<String, String>> resultKeyword = new ArrayList<>();
        if (!listArea.isEmpty()) {
            resultArea = delicacyMapper.getDelicacyByArea(listArea, Low, High);
        }
        else {resultArea = delicacyMapper.findAll(Low,High);}
        if (!listCuisine.isEmpty()) {
            resultCuisine = delicacyMapper.getDelicacyByCuisine(listCuisine,Low,High);
        }
        else resultCuisine = delicacyMapper.findAll(Low,High);
        if (!listKeyword.isEmpty()) {
            resultKeyword = delicacyMapper.getDelicacyByKeyword(listKeyword,Low,High);
        }
        else resultKeyword = delicacyMapper.findAll(Low,High);
        //三份数据取交集，得到最终的筛选
        for (Map area : resultArea){
            for (Map cusine : resultCuisine){
                if(cusine.equals(area)){
                    tmp.add(area);
                }
            }
        }
        for (Map keyword : resultKeyword){
            for (Map item : tmp){
                if(item.equals(keyword)){
                    target.add(keyword);
                }
            }
        }
        return target;
    }
}
